<template>
  <div>
    <!-- 角色展示 复选框组 -->
    <!-- checkList 选中复选框后,里面的值就是label中的值 -->
    <el-checkbox-group v-model="checkList">
      <!-- label 对应的就是当前选中的复选框的值 -->
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="setRoles">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { getRoles } from '@/api/setting'
import { getUserPhotoById } from '@/api/user'
import { assiginRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    userId: {
      type: [Number, String],
      required: true
    }
  },
  data() {
    return {
      checkList: [], // 复选框组-选中以后的值
      roleList: []
    }
  },
  created() {
    // 调用获取所有的角色列表
    this.getRoles()
    // 调用员工拥有的角色列表
    this.getUserPhotoById()
  },
  methods: {
    // 点击确认分类角色
    async setRoles() {
      const res = await assiginRoles({ id: this.userId, roleIds: this.checkList })
      this.$message.success(res.message)
      // this.getUserPhotoById()
      this.$emit('closeRoleDialog')
    },
    // 获取员工已经拥有的角色列表
    async getUserPhotoById() {
      const res = await getUserPhotoById(this.userId)
      this.checkList = res.data.roleIds === null ? [] : res.data.roleIds
    },
    // 获取所有的角色列表
    async getRoles() {
      const res = await getRoles()
      this.roleList = res.data.rows
    },
    // 点击取消
    cancleDialog() {
      this.$emit('closeRoleDialog')
    }
  }
}
</script>
<style scoped lang="scss">
.footer {
  margin-top: 30px;
}
</style>
